package com.tradesolver;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.helper.DAOBase;

public class Dao extends DAOBase {

  Logger logger = Logger.getLogger(Dao.class.getName());

  static {
    ObjectifyService.register(ListedCompany.class);
    ObjectifyService.register(StockUpdate.class);
    ObjectifyService.register(FinancialsText.class);
    ObjectifyService.register(FinancialsMap.class);
    ObjectifyService.register(TickerStat.class);
  }

  public Key<TickerStat> putTickerStat(TickerStat o) {
    return ofy().put(o);
  }

  public TickerStat getTickerStat(String ticker) {
    TickerStat result = null;
    try {
      result = ofy().get(new Key<TickerStat>(TickerStat.class, ticker));
    } catch (NotFoundException e) {
      logger.info("No TickerStat data found for ticker=" + ticker);
    }
    return result;
  }

  public Key<ListedCompany> putListedCompany(ListedCompany o) {
    return ofy().put(o);
  }

  public ListedCompany getListedCompany(String ticker) {
    ListedCompany result = null;
    try {
      result = ofy().get(new Key<ListedCompany>(ListedCompany.class, ticker));
    } catch (NotFoundException e) {
      logger.info("No data found for ticker=" + ticker);
    }
    return result;
  }

  public Key<StockUpdate> putStockUpdate(StockUpdate o) {
    o.insertDate = new Date();
    return ofy().put(o);
  }

  public Key<FinancialsText> putFinancialsText(FinancialsText o) {
    o.updateDate = new Date();
    return ofy().put(o);
  }

  public FinancialsText getFinancialsText(String ticker) {
    FinancialsText result = null;
    try {
      result = ofy().get(new Key<FinancialsText>(FinancialsText.class, ticker));
    } catch (NotFoundException e) {
      logger.info("No FinancialsText data found for ticker=" + ticker);
    }
    return result;
  }

  public Key<FinancialsMap> putFinancialsMap(FinancialsMap o) {
    o.updateDate = new Date();
    return ofy().put(o);
  }

  public FinancialsMap getFinancialsMap(String ticker) {
    FinancialsMap result = null;
    try {
      result = ofy().get(new Key<FinancialsMap>(FinancialsMap.class, ticker));
    } catch (NotFoundException e) {
      logger.info("No FinancialsMap data found for ticker=" + ticker);
    }
    return result;
  }

  public List<FinancialsMap> getFinancialsMaps() {
    logger.fine("Querying getFinancialsMaps");
    return ofy().query(FinancialsMap.class).list();
  }

  public List<StockUpdate> getStockUpdates(String stampDate) {
    logger.fine("Querying stock updates for:" + stampDate);
    return ofy().query(StockUpdate.class).filter("stampDate", stampDate).list();
  }
}